package cn.mdmbct.spring.starter.douyin.common.validator;

import cn.mdmbct.douyin.common.exception.InvalidOpenApiPropertiesException;
import cn.mdmbct.spring.starter.douyin.common.annotation.NotNull;
import cn.mdmbct.spring.starter.douyin.common.properties.dy.AppProperties;

import java.lang.reflect.Field;

/**
 * open api配置检查
 *
 * @author mdmbct  mdmbct@outlook.com
 * @date 2021/3/9 14:27
 * @modified mdmbct
 * @since 1.0
 */
public class DyOpenAppValidator {

    public static void isValid(AppProperties properties) {

        if (null == properties) {
            throw new InvalidOpenApiPropertiesException("未配置抖音Open API！");
        }

        Field[] fields = properties.getClass().getDeclaredFields();
        StringBuilder stringBuilder = new StringBuilder("抖音Open API中的 ");
        boolean flag = false;
        for (Field field : fields) {
            try {
                field.setAccessible(true);
                if (field.isAnnotationPresent(NotNull.class) && field.get(properties) == null) {
                    stringBuilder.append(field.getName()).append(' ');
                    flag = true;
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        if (flag) {
            stringBuilder.append("属性不能为空！");
            throw new InvalidOpenApiPropertiesException(stringBuilder.toString());
        }
    }

}
